#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);



    this->setMaximumSize(260,340);
    this->setMinimumSize(260,340);


    this->setWindowTitle("计算器");



    QIcon con("D:\\12\\Calculator\\tt.png");
    ui->cancelonebutton->setIcon(con);



    ui->amountbutton->setStyleSheet("background:skyblue");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_zerobutton_clicked()
{
    str+="0";
    ui->lineEdit->setText(str);
}

void Widget::on_onebutton_clicked()
{
    str+="1";
    ui->lineEdit->setText(str);
}

void Widget::on_twobutton_clicked()
{
    str+="2";
    ui->lineEdit->setText(str);
}

void Widget::on_threebutton_clicked()
{
    str+="3";
    ui->lineEdit->setText(str);
}

void Widget::on_fourbutton_clicked()
{
    str+="4";
    ui->lineEdit->setText(str);
}

void Widget::on_fivebutton_clicked()
{
    str+="5";
    ui->lineEdit->setText(str);
}

void Widget::on_sixbutton_clicked()
{
    str+="6";
    ui->lineEdit->setText(str);
}

void Widget::on_sevenbutton_clicked()
{
    str+="7";
    ui->lineEdit->setText(str);
}

void Widget::on_eightbutton_clicked()
{
    str+="8";
    ui->lineEdit->setText(str);
}

void Widget::on_ninebutton_clicked()
{
    str+="9";
    ui->lineEdit->setText(str);
}

void Widget::on_addbutton_clicked()
{
    str+="+";
    ui->lineEdit->setText(str);
}

void Widget::on_subbutton_clicked()
{
    str+="-";
    ui->lineEdit->setText(str);
}

void Widget::on_mulbutton_clicked()
{
    str+="*";
    ui->lineEdit->setText(str);
}


void Widget::on_divbutton_clicked()
{
    str+="/";
    ui->lineEdit->setText(str);
}

void Widget::on_leftbutton_clicked()
{
    str+="(";
    ui->lineEdit->setText(str);
}

void Widget::on_rightbutton_clicked()
{
    str+=")";
    ui->lineEdit->setText(str);
}

void Widget::on_cancelonebutton_clicked()
{
    str.chop(1);
     ui->lineEdit->setText(str);
}

void Widget::on_cancelallbutton_clicked()
{
    str.clear();
     ui->lineEdit->setText(str);
}
int Widget::Priority(char ch)
{
    switch(ch)
    {
        case '(':
            return 3;
        case '*':
        case '/':
            return 2;
        case '+':
        case '-':
            return 1;
        default:
            return 0;
    }
}

void Widget::on_amountbutton_clicked()
{
    QStack<int> s_num, s_opt;

    char opt[128] = {0};
    int i = 0, tmp = 0, num1, num2;

    //把QString转换成char *
    QByteArray ba;
    ba.append(str);   //把QString转换成QByteArray
    strcpy(opt, ba.data());  //data可以把QByteArray转换成const char *

    while (opt[i] != '\0' || s_opt.empty() != true)
    {
        if (opt[i] >= '0' && opt[i] <= '9')
        {
            tmp = tmp * 10 + opt[i] - '0';
            i++;
            if (opt[i] < '0' || opt[i] > '9')
            {
                s_num.push(tmp);
                tmp = 0;
            }
        }
        else           //操作符
        {
            if (s_opt.empty() == true || Priority(opt[i]) > Priority(s_opt.top()) ||
                    (s_opt.top() == '(' && opt[i] != ')'))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }

            if (s_opt.top() == '(' && opt[i] == ')')
            {
                s_opt.pop();
                i++;
                continue;
            }

            if (Priority(opt[i]) <= Priority(s_opt.top()) || (opt[i] == ')' && s_opt.top() != '(') ||
                (opt[i] == '\0' && s_opt.empty() != true))
            {
                char ch = s_opt.top();
                s_opt.pop();
                /*减法和除法，先出栈的作为第二个参数   后缀表达式*/
                switch(ch)
                {
                    case '+':
                        num1 = s_num.top();
                        s_num.pop();
                        num2 = s_num.top();
                        s_num.pop();
                        s_num.push(num1 + num2);
                        break;
                    case '-':
                        num1 = s_num.top();
                        s_num.pop();
                        num2 = s_num.top();
                        s_num.pop();
                        s_num.push(num2 - num1);
                        break;
                    case '*':
                        num1 = s_num.top();
                        s_num.pop();
                        num2 = s_num.top();
                        s_num.pop();
                        s_num.push(num1 * num2);
                        break;
                    case '/':
                        num1 = s_num.top();
                        s_num.pop();
                        num2 = s_num.top();
                        s_num.pop();
                        s_num.push(num2 / num1);
                        break;
                }
            }
        }
    }
    ui->lineEdit->setText(QString::number(s_num.top()));
    str.clear();
}


